<template>
  <basic-container class="wf-container">
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :search.sync="query"
               :permission="permissionList"
               v-model="form"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="onLoad(page, query)"
               @on-load="onLoad">
      <template slot="menu"
                slot-scope="{row}">
        <el-button v-if="permission.wf_process_todo_detail"
                   type="text"
                   size="small"
                   icon="el-icon-info"
                   @click="dynamicRoute(row, 'detail')">详情</el-button>
        <el-button v-if="permission.wf_process_todo_follow"
                   type="text"
                   size="small"
                   icon="el-icon-search"
                   @click="handleFlow(row)">流程图</el-button>
      </template>
      <template #searchMenu
                v-if="permission.wf_process_todo_search">
        <el-badge :value="query.formSearch? query.formSearch.split(',').length: 0"
                  :hidden="!query.formSearch || query.formSearch.split(',').length == 0">
<!--          <el-button icon="el-icon-search"
                     @click="$refs['wf-search'].visible = true">表单</el-button>-->
        </el-badge>
      </template>
    </avue-crud>

    <el-dialog :visible.sync="bpmnVisible"
               append-to-body
               destroy-on-close
               title="流程图"
               width="70%"
               custom-class="wf-dialog">
      <wf-design ref="bpmn"
                 style="height: 60vh;"
                 :options="bpmnOption"></wf-design>
    </el-dialog>
    <wf-search ref="wf-search"
               v-model="query.formSearch"></wf-search>
  </basic-container>
</template>

<script>
import { todoList as getList, detail } from "@/api/plugin/workflow/process";
import { mapGetters } from "vuex";

import exForm from '../mixins/ex-form'

import WfSearch from './components/search.vue'

export default {
  mixins: [exForm],
  components: { WfSearch },
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        size: 'mini',
        height: 'auto',
        calcHeight: 30,
        tip: false,
        border: true,
        selection: true,
        dialogType: 'drawer',
        addBtn: false,
        editBtn: false,
        delBtn: false,
        align: 'center',
        searchMenuSpan: 6,
        searchSize: 'mini',
        searchIndex: 3,
        searchIcon: false,

        column: [
          {
            label: '任务类型',
            prop: 'processDefinitionKey',
            search: true,
            type: "select",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "string",
            dicUrl: "/api/blade-system/dict/dictionary?code=platform_process_key",
            width: 80,
            hide: true
          },
          {
            label: '任务名称',
            prop: 'processDefinitionName',
            search: true,
            overHidden: true
          },
          {
            label: '当前节点',
            prop: 'taskName',
          },
          {
            label: '申请人',
            search: true,
            prop: 'startUsername',
          },
          {
            label: '申请时间',
            prop: 'createTime',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm',
            width: 165,
          },
          {
            label: "申请时间",
            prop: "timeData",
            type: "datetimerange",
            searchRange: true,
            defaultTime:['00:00:00', '23:59:59'],
            format:'yyyy-MM-dd HH:mm:ss',
            valueFormat:'yyyy-MM-dd HH:mm:ss',
            startPlaceholder: "开始时间",
            endPlaceholder: "结束时间",
            search: true,
            rules: [
              {
                required: true,
                message: "请选择统计时间段",
                trigger: "blur",
              },
            ],
            hide: true,
            display: false,
            searchSpan: 12,
            overHidden: true
          },
        ]
      },
      data: [],
      bpmnVisible: false,
      bpmnOption: {}
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.deployment_add, false),
        viewBtn: this.vaildData(this.permission.deployment_view, false),
        delBtn: this.vaildData(this.permission.deployment_delete, false),
        editBtn: this.vaildData(this.permission.deployment_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  watch: {
    "query.timeData"(val){
      if(val){
        this.query.createTime = val[0];
        this.query.endTime = val[1];
      }else{
        this.query.createTime = "";
        this.query.endTime = "";
      }
    }
  },
  methods: {
    handleDetail(row) {
      const param = {
        taskId: row.taskId,
        processInsId: row.processInstanceId
      }
      this.$router.push('/workflow/process/detail/' + Buffer.from(JSON.stringify(param)).toString('base64'))
    },
    handleFlow(row) {
      const { taskId, processInstanceId } = row
      detail({ taskId, processInsId: processInstanceId }).then(res => {
        const { process, flow } = res.data.data

        this.bpmnOption = {
          mode: 'view',
          xml: process.xml,
          flows: this.handleResolveFlows(flow)
        }

        this.bpmnVisible = true
      })
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    }
  }
};
</script>

<style lang="scss">
.wf-container {
  .el-collapse-item__content {
    padding-top: 8px;
  }
  .el-badge__content.is-fixed {
    right: 16px;
  }
}
.wf-dialog {
  display: flex;
  flex-direction: column;
  margin: 0 !important;
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -40%);
  max-height: calc(100% - 30px);
  max-width: calc(100% - 30px);
  .el-dialog__body {
    flex: 1;
    overflow: auto;
  }
}
</style>
